package yanyan.com.tochar;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import okhttp3.OkHttpClient;
import yanyan.com.tochar.adpter.FullyGridLayoutManager;
import yanyan.com.tochar.adpter.GridImageAdapter;
import yanyan.com.tochar.beans.ColorBean;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.PictureSelectUtil;
import yanyan.com.tochar.utils.ToastUtil;

/**
 * 以图制图
 */
public class MoreImageToImgActivity  extends Activity {
    private List imgList= new ArrayList<>();//图片集合
    private RecyclerView recyclerView;
    private GridImageAdapter adapter;
    private Map<Integer,Integer> colorMap=new HashMap<>();
    private List<ColorBean> colorBeans;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_image);
        recyclerView=findViewById(R.id.test_recycler);


        //初始化图片选择
        FullyGridLayoutManager manager = new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        adapter = new GridImageAdapter(this, new GridImageAdapter.onAddPicClickListener() {
            @Override
            public void onAddPicClick() {
                PictureSelectUtil.selectPohto(MoreImageToImgActivity.this,3306);
            }
        });
        adapter.setList(imgList);
        adapter.setSelectMax(1);
        recyclerView.setAdapter(adapter);
    }



    public void OneTest(View view){
        if(imgList!=null && imgList.size()>0){
            LocalMedia localMedia= (LocalMedia) imgList.get(0);
            String path="";
            if(CoreUtil.isAndroidQ()){
                path=localMedia.getAndroidQToPath();
            }else {
                path= localMedia.getPath();
            }
            Bitmap bitmap = PictureSelectUtil.getBitmap(path);
            showPix(bitmap);
            ToastUtil.showLongToast(this,"长度:"+colorBeans.size());

        }else {

        }
    }

    public void showPix(Bitmap bitmap){
        colorBeans=new ArrayList<>();
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        for (int x=0;x<width;x+=2){
            if(x>width){
                continue;
            }
            for (int y=0;y<height;y+=2){
                if(y>height){
                    continue;
                }
                int pixel1 = bitmap.getPixel(x, y);
                int pixel2=pixel1;
                int pixel3=pixel1;
                int pixel4=pixel1;
                //如果有一个坐标越界,则用第一个像素点的颜色值
                if(x+1<=width || y+1<=height){
                    pixel2 = bitmap.getPixel(x+1, y);
                    pixel3 = bitmap.getPixel(x, y+1);
                    pixel4 = bitmap.getPixel(x+1, y+1);
                }
                int color = comColor(pixel1, pixel2, pixel3, pixel4);
                boolean isEnd= (y>=height);
                ColorBean colorBean=new ColorBean(color,isEnd);
                colorBeans.add(colorBean);
            }
        }
    }

    public int comColor(int pix1,int pix2,int pix3,int pix4){
        if(pix1==pix2 || pix1==pix3 || pix1==pix4){
            return pix1;
        }else if(pix2==pix3 || pix2==pix4){
            return pix2;
        }else if(pix3==pix4){
            return pix3;
        }else {
            return pix1;
        }
    }




    /**
     * 选择图片事件
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            imgList = PictureSelector.obtainMultipleResult(data);
            adapter.setList(imgList);
            adapter.notifyDataSetChanged();
        }
    }
}
